using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using FetCo.Data;
using ADESSA.Business;
using ADESSA.Entities;
using FetCo.Framework;
using FetCo.Web;
using System.IO;

public partial class miniweb_ProductDetail : System.Web.UI.Page
{
    #region Viarible
    protected long Id
    {
        get
        {
            object o = ViewState["Id"];
            if (o != null)
                return (long)o;
            return long.MinValue;
        }
        set
        {
            ViewState["Id"] = value;
        }
    }
    #endregion
    protected string emailCompany = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        HtmlAnchor aMenuProduct = (HtmlAnchor)Page.Master.FindControl("aMenuProduct");
        aMenuProduct.Attributes.Add("class", "Select");
        if (Request.QueryString["id"] != null)
        {
            this.Id = long.Parse(Request.QueryString["id"].ToString());
            loadData(Id);
           

        }

    }
    
    private void loadData(long IdPro)
    {

        ProductSell _ProductSell = ProductSellService.GetProductSell(IdPro);
        if (_ProductSell != null)
        {
            Literal ltrPageTitle = (Literal)Master.FindControl("ltrPageTitle");
            ltrPageTitle.Text = _ProductSell.Title;
            ltrTitlePro.Text = _ProductSell.Title;
            ltrTitle.Text = _ProductSell.Title;
            if (!string.IsNullOrEmpty(_ProductSell.ThumbnailImage))
            {
                imgPro.Src = "../photos/" + _ProductSell.ThumbnailImage;
            }
            ltrDescription.Text = _ProductSell.Description;
            ltrEnd.Text = _ProductSell.ProductDescription;
            if (_ProductSell.CatId != 0)
                loadOtherProduct(_ProductSell.ProductId, _ProductSell.CatId);
        }
    }
    #region Load other product
    private void loadOtherProduct(long idPro, long catPro)
    {
        Criteria cri = new Criteria();
        cri.add(ProductSell._Table + "." + ProductSellColumns.CatId, catPro, Condition.EQUAL, Condition.AND);
        cri.add(ProductSell._Table + "." + ProductSellColumns.ProductId, idPro, Condition.NOT_EQUAL, Condition.AND);
        ProductSellCollection _ProductSellCollection = ProductSellService.GetProductSellListByParam(ProductSellColumns.CreatedDate, OrderDirection.DESC, cri.Criter);
        if (_ProductSellCollection.Count > 0)
        {
            rptPro.DataSource = _ProductSellCollection;
            rptPro.DataBind();
        }
    }

    protected void rptPro_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        ProductSell _ProductSell = (ProductSell)e.Item.DataItem;
        HtmlImage imgPro = (HtmlImage)e.Item.FindControl("imgPro");
        Literal ltrTitlePro = (Literal)e.Item.FindControl("ltrTitlePro");
        HtmlAnchor ahrImg = (HtmlAnchor)e.Item.FindControl("ahrImg");
        HtmlAnchor ahrTitle = (HtmlAnchor)e.Item.FindControl("ahrTitle");
        if (!string.IsNullOrEmpty(_ProductSell.Image))
        {
            imgPro.Src = "../photos/" + _ProductSell.Image;
        }
        ahrImg.HRef = string.Format("ProductDetail.aspx?id={0}", _ProductSell.ProductId.ToString());
        ahrTitle.HRef = string.Format("ProductDetail.aspx?id={0}", _ProductSell.ProductId.ToString());
        ltrTitlePro.Text = Utilitys.ShortSummary(20, _ProductSell.Title);
        imgPro.Alt = _ProductSell.Title;
        imgPro.Attributes.Add("Title", _ProductSell.Title);
    }
    #endregion
    #region Send Mail
    #region Send Contact
    #region Get Email Send To Admin
    private string EmailSend()
    {


        Criteria criuser = new Criteria();
        ADESSA.Entities.UserCollection _UserCollection = UserService.GetUserListByParam(UserColumns.UserId, OrderDirection.ASC, criuser.Criter);
        User _User = new User();
        if (_UserCollection.Count > 0)
        {
            _User = _UserCollection[0];
            return _User.Email.ToString();
        }
        else
            return RTISettings.EmailAdministrator;

    }
    #endregion


    #region Send Mail
    protected void SendMail(string email, string namecompany, string content)
    {
        Criteria _cri = new Criteria();
        _cri.add("[" + NewsletterColumns.Order + "]", FetConstant.EmailSendContactForCompany, Condition.EQUAL, Condition.AND);
        NewsletterCollection _NewsletterCollection = NewsletterService.GetNewsletterListByParam("[" + Newsletter._Table + "].[" + NewsletterColumns.CreatedDate + "]", OrderDirection.ASC, _cri.Criter);
        if (_NewsletterCollection != null)
        {
            if (_NewsletterCollection.Count > 0)
            {

                string subject = _NewsletterCollection[0].Title.Replace("Email_", "").ToString();

                string mailContent = string.Format(_NewsletterCollection[0].Content, namecompany, content);
                if (!Utility.SendMailDisplay(email, subject, mailContent, FetConstant.DisplayNameEmail, EmailSend()))
                {
                    Alert.Show("Send email for company unsussecc");
                    return;
                }
                if (!Utility.SendMailDisplay(EmailSend(), subject, mailContent, FetConstant.DisplayNameEmail, email))
                {
                    Alert.Show("Send email for company unsussecc");
                    return;
                }
                johndoe.Value = string.Empty;
                Alert.Show("Send email for company sussecc");
            }
        }



    }

    #endregion

    #endregion
    #endregion
    protected void btnSend_Click(object sender, EventArgs e)
    {
        if (Session["CompanyID"] != null)
        {

            this.Id = long.Parse(Session["CompanyID"].ToString());
            Company _Company = CompanyService.GetCompany(this.Id);
            if (_Company != null)
            {
              
                SendMail(_Company.Email, _Company.CompanyName, johndoe.Value);
            }
        }
    }
}
